/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import thecliffproblem.Action;
import thecliffproblem.Board;
import thecliffproblem.Coordinate;

/**
 *
 * @author Jaime
 */
public class BoardEvaluationPanel extends JPanel{
    
    private SquareEvaluationPanel[][] panels;
    private Board board;
    
    public BoardEvaluationPanel(Board board)
    {
        this.board = board;
        board.registerObserver(new BoardEvaluationPanelObserver());
        
        setLayout(new GridLayout(board.getNumberOfLines(), board.getNumberOfColumns()));
        
        panels = new SquareEvaluationPanel[board.getNumberOfLines()][board.getNumberOfColumns()];
        
        for(int i = 0; i < board.getNumberOfLines(); i++)
        {
            for(int j = 0; j < board.getNumberOfColumns(); j++)
            {
                SquareEvaluationPanel sep = new SquareEvaluationPanel();
                panels[i][j] = sep;
                add(sep);
            }
        }        
        
        setBorder(new TitledBorder("Evaluations"));
        
    }

    void repaintAll() {
        
        for(int i = 0; i < board.getNumberOfLines(); i++)
        {
            for(int j = 0; j < board.getNumberOfColumns(); j++)
            {
                panels[i][i].revalidate();
                panels[i][j].repaint();
            }
        }
     
        revalidate();
        repaint();

    }
    
    private class BoardEvaluationPanelObserver implements ActionObserver
    {

        @Override
        public void step(Coordinate origin, Coordinate destination) {
            
        }

        @Override
        public void episodeEnd() {
            
        }

        @Override
        public void newEvaluation(Action action, Coordinate square, double value) {
            
            panels[square.getLine()][square.getColumn()].setValue(action, value);
            //repaintAll();
           
            
        }
        
    }
    
    
    
}
